package com.googlecode.aleverr.yafotkiclient.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.googlecode.aleverr.yafotkiclient.R;
import com.googlecode.aleverr.yafotkiclient.api.Photo;

public class PhotoViewerActivity extends Activity {

    private static final String TAG = "PhotoViewerActivity";

    private static final int IMAGE_VIEW_INDEX = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_viewer);

        Intent intent = getIntent();
        Photo photo = (Photo) intent.getExtras().get("photo");

        TextView photoTitle = (TextView) findViewById(R.id.photoTitle);
        photoTitle.setText(photo.getTitle() != null ? photo.getTitle() : "");

        new LoadPhotoTask().execute(photo);
    }

    private class LoadPhotoTask extends AsyncTask<Photo, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Photo... photos) {
            if (photos.length > 0) {
                return photos[0].getMBitmap();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                ImageView imageView = (ImageView) findViewById(R.id.photoViewerPhoto);
                imageView.setImageBitmap(bitmap);
            }
            ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
            viewSwitcher.setDisplayedChild(IMAGE_VIEW_INDEX);
        }
    }
}